package com.lz.football.module.ladderMatch.exchange
{
	import com.lz.football.module.base.IComponent;
	
	import common.BaseView.BaseView;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import lz.controls.core.TitleWindow;
	import lz.controls.nick.PagePanel;
	
	public class ExchangeBasePanel extends BaseView implements IComponent
	{
		/**
		 *显示长度 
		 */
		public const ITEMLENGTH:int = 9;
		/**
		 *item容器 
		 */
		public var _contain:MovieClip;
		/**
		 *换页  
		 */
		public var _page:PagePanel;
		/**
		 *兑换按钮 
		 */
		public var _exchangeBtn:DisplayObject;
		/**
		 *页容器 
		 */
		public var _pageContain:MovieClip;
		/**
		 *选中的item 
		 */
		public var _selectItem:IExchangeItem;
		public var _itemVec:Vector.<IExchangeItem>;
		/**
		 * 
		 */
		
		public var _goodVec:Array;
		public function ExchangeBasePanel()
		{
			super();
		}
		
		override public function initUI(view:Object=null):void
		{
			setCloseBtn();
			_contain = symbolSkin._contain;
			_pageContain = symbolSkin._pageContain;
			_page = new PagePanel();
			_page.initUI("com.lz.commonUI.turnPage_white");
			_page.callBack = changePage;
			_pageContain.addChild(_page);
			_exchangeBtn = symbolSkin._exchangeBtn;
			setItem();
		}
		public function setItem():void
		{
			if(_itemVec != null)return;
			_itemVec = new Vector.<IExchangeItem>();
			var item:ExchangeItem;
			for(var i:int = 0;i < ITEMLENGTH;i++)
			{
				item = new ExchangeItem();
				item.x = (item.width + 30) * (i % 3);
				item.y = (item.height + 10)* int(i / 3);
				
				item.callBack = selectItem;
				_itemVec.push(item);
				_contain.addChild(item);
			}
		}
		public function selectItem($item:IExchangeItem):void
		{
			if(_selectItem != null)_selectItem.select = false;
			_selectItem = $item;
			_selectItem.select = true;
		}
		/**
		 *换页  
		 * @param $str
		 * 
		 */
		public function changePage($str:String):void
		{
			showItem();
		}
		/**
		 *显示 
		 * 
		 */
		public function showItem():void
		{
			reset();
			var i:int = (_page.min - 1) * ITEMLENGTH;
			var len:int =  (_page.min * ITEMLENGTH) < _goodVec.length? _page.min * ITEMLENGTH: _goodVec.length;
			var j:int;
			for(i;i < len;i++)
			{
				_itemVec[j].setDate(_goodVec[i]);
				j++;
			}
			
		}
		/**
		 * 重置 
		 * 
		 */
		public function reset():void
		{
			for(var i:int = 0;i < ITEMLENGTH;i++)
			{
				_itemVec[i].reSet();
			}
			_selectItem = null;
		}
		/**
		 *兑换 
		 * @param event
		 * 
		 */
		public function onExchangeHandler(event:MouseEvent):void
		{
//			if(_selectItem == null)
//			{
//				Alert.showErrorTip("请选择要兑换的物品");
//				return;
//			}
//			if(_selectItem.goodVo.honour > int(_honourValue.text))
//			{
//				Alert.showErrorTip("荣誉不足，不可以购买此物品");
//				return;
//			}
//			this.dispatchEvent(new StadiumEvent(StadiumEvent.EXCHANGE_HONOUR,{pid:_selectItem.goodVo.pid,num:1},true));
		}
		override public function addEvtListener():void
		{
			_exchangeBtn.addEventListener(MouseEvent.CLICK,onExchangeHandler);
		}
		
		override public function removeEvtListener():void
		{
		}
		
		override public function destroyView():void
		{
			removeEvtListener();
			if(this.parent)
				this.parent.removeChild(this);
			while(_itemVec.length)
			{
				_itemVec.pop().destroyView();
			}
//			if(_goodVec != null)_goodVec.splice(0,_goodVec.length);
			_goodVec = null;
			_page.destroyView();
			symbolSkin = null;
		}
	}
}